package com.intretech.umsin.security.satoken.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @version V1.0
 * @ClassName: SecurityProperties
 * @Description: 功能描述 Security 配置属性
 * @author: 李启联
 * @date: 2022-8-5 16:00
 * @Copyright: Copyright (c) 2022
 */

@Component
@ConfigurationProperties(prefix = "security")
public class SecurityProperties {

    /**
     * 监控中心和swagger需要访问的url
     */
    private static final String[] ENDPOINTS = {
            "/**/actuator/**", "/**/actuator/**/**",  //端点监控
            "/**/doc.html", "/doc.html",  //端点监控
            "/v2/api-docs/**", "/swagger-ui.html", "/swagger-resources/**", "/webjars/**",            // api-gateway webflux swagger
            "/**/v2/api-docs/**", "/**/swagger-ui.html", "/**/swagger-resources/**", "/**/webjars/**", //业务中心swagger
            "/**/turbine.stream", "/**/turbine.stream**/**", "/**/hystrix", "/**/hystrix.stream", "/**/hystrix/**", "/**/hystrix/**/**", "/**/proxy.stream/**", //熔断监控
            "/**/druid/**", "/**/favicon.ico", "/**/prometheus"
    };

    /**
     * 排除路径
     */
    private String[] excludes;


    /**
     *
     * @return 自定义的url和监控中心需要访问的url集合
     */
    public String[] getExcludes() {
        if (excludes == null || excludes.length == 0) {
            return ENDPOINTS;
        }

        List<String> list = new ArrayList<>();
        for (String url : ENDPOINTS) {
            list.add(url);
        }
        for (String url : excludes) {
            list.add(url);
        }

        return list.toArray(new String[list.size()]);
    }

    public void setExcludes(String[] excludes) {
        this.excludes = excludes;
    }

    public SecurityProperties() {
    }

    @Override
    public String toString() {
        return "SecurityProperties{" +
                "excludes=" + Arrays.toString(excludes) +
                '}';
    }
}